package com.forg.mvc.controller;

import java.util.HashMap;
import java.util.Map;

import com.forg.mvc.core.RequestMethodEnum;
import com.forg.mvc.core.ResultTypeEnum;
import com.forg.mvc.core.annotation.Controller;
import com.forg.mvc.core.annotation.RenderType;
import com.forg.mvc.core.annotation.RequestMapping;
import com.forg.mvc.core.common.RequestUtil;
import com.forg.mvc.core.common.groovy.GroovyCompiler;

@Controller
public class HelloController {

	@RequestMapping(method=RequestMethodEnum.GET,value="/hello.do")
	@RenderType(type=ResultTypeEnum.TEXT)
	public String hello(){
		
		return "hello,xiaominmin~";
	}
	
	@RequestMapping(method=RequestMethodEnum.POST,value="/hello.do")
	public String posthello(Map<String,String> param){
		
		return param.toString();
	}
	
	@RequestMapping(method=RequestMethodEnum.GET,value="/hello2.do")
    @RenderType(type=ResultTypeEnum.JSON)
    public Map<String,String> hello2(){
        
	    Map<String,String > ret = new HashMap<>();
	    ret.put("name", "Minmin");
	    ret.put("age", "18");
	    
        return ret;
    }
	
	@RequestMapping(method=RequestMethodEnum.GET,value="/hello3.do")
    @RenderType(type=ResultTypeEnum.VELOCITY,tpl="hello.vm")
    public Map<String,String> hello3(){
        
        Map<String,String > ret = new HashMap<>();
        ret.put("name", "Minmin");
        ret.put("age", "18");
        
        return ret;
    }
	
	@RequestMapping(method=RequestMethodEnum.GET,value="/hellogroovy.do")
    @RenderType(type=ResultTypeEnum.JSON)
    public Object hellogroovy(){
        
	    Map<String,String> content = new HashMap<>();
	    content.put("money", "10000");
	    
        Object ret = GroovyCompiler.getClass("script/SayHello.groovy").get(content);
        
        return ret;
    }
	
	@RequestMapping("/hello4.do")
    @RenderType(type=ResultTypeEnum.JSON)
    public Map<String,Object> hello4(){
        
        Map<String,Object > ret = new HashMap<>();
        ret.put("method", RequestUtil.getMethod().getName());
        ret.put("path", RequestUtil.getPath());
        ret.put("param", RequestUtil.getParam().toString());
        
        ret.put("name", RequestUtil.getParamStr("name"));
        
        return ret;
    }
	
}
